using System;
using System.Globalization;
using System.Reflection;

namespace UnitTesting.Fixturing
{
	public static class ActivatorManager
	{
		static IActivator _current = DefaultActivator.Instance;

		public static IActivator Current
		{
			get { return _current; }
		}

		public static void SetCurrentAsDefault()
		{
			SetCurrent(null);
		}

		public static void SetCurrent(IActivator activator)
		{
			_current = activator ?? DefaultActivator.Instance;
		}

		public static void SetCurrentAsRhinoMock(Assembly rhinoMockAssembly)
		{
			_current = new RhinoMockActivator(rhinoMockAssembly);
		}

		public static void SetCurrentAsMoq(Assembly moqAssembly)
		{
			_current = new MoqActivator(moqAssembly);
		}

		internal static Type GetTargetType(Assembly assembly, string targeTypeName)
		{
			Ensure.IsNotNull(() => assembly);
			var result = assembly.GetType(targeTypeName);
			if (result == null)
			{
				throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, "The assembly '{0}' doest not contain the type '{1}'.", assembly, targeTypeName), "assembly");
			}
			return result;
		}
	}
}